﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MassIntellect.WorkIt
{
	public class TurnCrossPipe : CrossPipe
	{
		private List<PulseArgs> buff = new List<PulseArgs>();

		public override void Activate()
		{
			int i = 0;
#if !DEBUG
            try
            {
#endif
			for (i = 0; i < buff.Count; i++)
				InPipe.Pulse(buff[i]);
			buff.Clear();
#if !DEBUG
            }
            catch
            {
                buff.RemoveRange(0, i + 1);
                throw;
            }
#endif
		}

		public override bool IsActive
		{
			get
			{
				return buff.Count > 0;
			}
			set
			{
				base.IsActive = value;
			}
		}

		protected override bool BeforeConnect()
		{
			OutPipe.OnPulse += OutPipe_OnPulse;
			return true;
		}

		protected override bool BeforeDisconnect()
		{
			OutPipe.OnPulse -= OutPipe_OnPulse;
			return true;
		}

		private void OutPipe_OnPulse(PulseArgs subj)
		{
			buff.Add(subj);
		}
	}


}
